package gui;

import gui.customizepanel.JPicturePanel;

import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import controller.BtnLoginAction;
import controller.BtnShowRegisterAction;

public class IndexUI extends JFrame {

	private static final long serialVersionUID = 0xA1L;

	private JButton btn_login;
	private JButton btn_register;
	private JLabel lbl_password;
	private JLabel lbl_username;
	private JPanel pnForm;
	private JPicturePanel pn_logo;
	private JPasswordField tf_password;
	private JTextField tf_username;

	private static IndexUI instance;

	public static final IndexUI getInsntace() {
		if (instance == null)
			return instance = new IndexUI();
		return instance;
	}

	private IndexUI() {
		initComponents();
		events();
	}

	private void events() {
		btn_login.addActionListener(new BtnLoginAction(this));
		btn_register.addActionListener(new BtnShowRegisterAction(this));
	}

	private void newComponents() {
		pnForm = new JPanel();
		lbl_username = new JLabel();
		lbl_password = new JLabel();
		tf_username = new JTextField();
		tf_password = new JPasswordField();
		btn_login = new JButton();
		btn_register = new JButton();
		pn_logo = new JPicturePanel();
	}

	private void setWindowsProperties() {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setIconImage(new ImageIcon("src/icons/soft_icon_32x32.png").getImage());
		setTitle("Login");
		setResizable(false);
	}

	private void setComponentsProperties() {
		pnForm.setBorder(BorderFactory.createTitledBorder(""));

		lbl_username.setText("Username");

		lbl_password.setText("Password");

		btn_login.setText("Login");

		btn_register.setText("Register");
	}

	private void pnFormLayout() {
		GroupLayout pnFormLayout = new GroupLayout(pnForm);
		pnForm.setLayout(pnFormLayout);
		pnFormLayout
				.setHorizontalGroup(pnFormLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pnFormLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnFormLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addComponent(
																lbl_username)
														.addComponent(
																lbl_password))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pnFormLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addGroup(
																pnFormLayout
																		.createSequentialGroup()
																		.addComponent(
																				btn_login,
																				GroupLayout.PREFERRED_SIZE,
																				105,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18,
																				18,
																				Short.MAX_VALUE)
																		.addComponent(
																				btn_register,
																				GroupLayout.PREFERRED_SIZE,
																				96,
																				GroupLayout.PREFERRED_SIZE))
														.addComponent(
																tf_username)
														.addComponent(
																tf_password))
										.addContainerGap()));

		pnFormLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {
				btn_login, btn_register });

		pnFormLayout
				.setVerticalGroup(pnFormLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pnFormLayout
										.createSequentialGroup()
										.addGap(34, 34, 34)
										.addGroup(
												pnFormLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_username)
														.addComponent(
																tf_username,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pnFormLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_password)
														.addComponent(
																tf_password,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnFormLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(btn_login)
														.addComponent(
																btn_register))
										.addContainerGap(38, Short.MAX_VALUE)));
	}

	private void pn_logoLayout() {
		pn_logo.setBorder(BorderFactory.createTitledBorder(""));

		GroupLayout pn_logoLayout = new GroupLayout(pn_logo);
		pn_logo.setLayout(pn_logoLayout);
		pn_logoLayout.setHorizontalGroup(pn_logoLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGap(0, 187, Short.MAX_VALUE));
		pn_logoLayout.setVerticalGroup(pn_logoLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));

	}

	private void mainLayout() {
		GroupLayout layout = new GroupLayout(getContentPane());

		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING)
				.addGroup(
						GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(pn_logo,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(pnForm,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.LEADING, false)
										.addComponent(pn_logo,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(pnForm,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
						.addContainerGap(GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		pack();
		this.setLocationRelativeTo(null);
	}

	private void initComponents() {
		newComponents();
		setWindowsProperties();
		setComponentsProperties();
		pnFormLayout();
		pn_logoLayout();
		mainLayout();

	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public JButton getBtn_login() {
		return btn_login;
	}

	public JButton getBtn_register() {
		return btn_register;
	}

	public JLabel getLbl_password() {
		return lbl_password;
	}

	public JLabel getLbl_username() {
		return lbl_username;
	}

	public JPanel getPnForm() {
		return pnForm;
	}

	public JPicturePanel getPn_logo() {
		return pn_logo;
	}

	public JPasswordField getTf_password() {
		return tf_password;
	}

	public JTextField getTf_username() {
		return tf_username;
	}

	public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			IndexUI.getInsntace().setVisible(true);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
